package com.piece.core.elasticsearch.config;

import com.piece.core.elasticsearch.properties.ElasticsearchProperties;
import com.piece.core.elasticsearch.support.ESClientPool;
import com.piece.core.elasticsearch.support.EsClientPoolFactory;
import com.piece.core.framework.util.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import java.util.List;

public class ElasticSearchAutoConfig {

    @Value("${spring.data.elasticsearch.enable:false}")
    private boolean enable;

    @Bean
    public ESClientPool esClientPool(ElasticsearchProperties elasticsearchProperties) {
        List<String> clusterNodes = elasticsearchProperties.getHosts();
        if (!enable || CollectionUtil.isEmpty(clusterNodes)) {
            return null;
        }

        return new ESClientPool(new EsClientPoolFactory(elasticsearchProperties));
    }
}
